package cn.agiledata.bank.common.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.agiledata.bank.account.service.AccountServiceFactory;
import cn.agiledata.bank.common.dao.ActionTypeTemplate;
import cn.agiledata.bank.common.dao.TransactionTemplate;
import cn.agiledata.bank.common.service.email.CancelMail;
import cn.agiledata.bank.common.service.email.Controversial;
import cn.agiledata.bank.common.service.email.SubAcctExMail;
import cn.agiledata.bank.common.service.email.TransactionEmail;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.ScoresHis;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.Transaction;

import com.vnv.infrastructure.email.service.VNVMail;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class SetUpUses {
	ActionTypeTemplate at = (ActionTypeTemplate) IPBAppFactory
			.getObj("actionTypeTemplate");

	TransactionTemplate tt = (TransactionTemplate) IPBAppFactory
			.getObj("transactionTemplate");

	public void sendSetup(Transaction tran, final SubAccount sub,
			final String constant, String fee, String trueFee, String url,
			ScoresHis sh) throws Exception {
		BankFactory vf = new AccountServiceFactory();
		final AccountService ass = vf.getBankBean("Impl");
		final Transaction tranInter = tran;

		saveMail(tranInter, WeatherUtil.getNameOrEmailOrMobileByAccount(sub
				.getAccount()), sub.getAccount().getEmail(), sub
				.getAccountName(), fee, trueFee, url, sh, sub);
	}

	private void saveMail(Transaction tran, String name, String email,
			String subName, String fee, String trueFee, String url,
			ScoresHis sh, SubAccount sub) {

		VNVMail mail = new VNVMail();
		Map vnvMap = new HashMap();
		String message = "您收到来自" + url + "的E-mail";
		vnvMap.put("account", WeatherUtil.getNameOrEmailOrMobileByAccount(tran
				.getDebit().getAccount()));
		vnvMap.put("amount", tran.getAmount());
		vnvMap.put("tranId", tran.getTransId());
		vnvMap.put("tranTime", DateUtil.date2String(tran.getBeginDate()));
		vnvMap.put("fee", fee);
		vnvMap.put("bankcard", tran.getBankSeq());
		vnvMap.put("message", message);
		String now = DateUtil.date2String(new Date());

		if (tran.getTransStatus()
				.equals(Constant.TRANS_STATUS_TRANSACTIONCLOSE)) {
			if (sub.getId().longValue() == tran.getDebit().getId().longValue()) {
				vnvMap.put("people", "收款人");
				vnvMap.put("anotherpeople", "付款子账户");
			} else {
				vnvMap.put("people", "收款对象");
			}
		} else {
			if (sub.getId().longValue() == tran.getDebit().getId().longValue()) {
				vnvMap.put("people", "收款人");
				vnvMap.put("anotherpeople", "付款子账户");
			} else {
				vnvMap.put("people", "付款人");
				vnvMap.put("anotherpeople", "收款子账户");
			}
		}
		vnvMap.put("trueFee", trueFee);
		vnvMap.put("subaccount", subName);

		mail.setEmailTo(tran.getCredit().getAccount().getEmail());

		mail.setSubject("您的付款通知");

		if (tran.getTransStatus().equals(Constant.TRANS_STATUS_REFUNDSUCC)) {
			/**
			 * 担保交易&退款成功 给买方发邮件
			 */
			TransactionEmail tranMail = (TransactionEmail) IPBAppFactory
					.getObj("transactionMail");
			tranMail.setUrl(url);

			tranMail.mail(tran.getDebit());
			if (tt.ifSendMail(tran.getDebit().getAccount(),
					Constant.CANCEL_SUCCESS_BUY))
				tranMail.sendMail(tran, null);
			if (tt.ifSendMobile(tran.getDebit().getAccount(),
					Constant.CANCEL_SUCCESS_BUY)) {
				// 同意退款，付款方短信
				Map mapParam = new HashMap();
				mapParam.put("credit", WeatherUtil
						.getNameOrEmailOrMobileByAccount(tran.getDebit()
								.getAccount()));
				mapParam.put("time", now);
				mapParam.put("amount", tran.getAmount().toString().substring(1));
				tranMail.sendSms(tran, null,"sms-refund_accept.vm",mapParam);
			}

		} else if (tran.getTransStatus().equals(Constant.TRANS_STATUS_DISPUTED)) {
			/**
			 * 担保交易&争议处理 给买方发邮件
			 */
			Controversial cs = (Controversial) IPBAppFactory
					.getObj("controversialMail");
			if (tt.ifSendMail(tran.getDebit().getAccount(), Constant.CHAOS_BUY))
				cs.sendMail(tran, null);
			if (tt.ifSendMobile(tran.getDebit().getAccount(),
					Constant.CHAOS_BUY)) {
				// 拒绝退款，付款方短信
				Map mapParam = new HashMap();
				mapParam.put("credit", WeatherUtil
						.getNameOrEmailOrMobileByAccount(tran.getDebit()
								.getAccount()));
				cs.sendSms(tran, null,"sms-refund_reject.vm",mapParam);
			}
		} else if (tran.getTransStatus().equals(
				Constant.TRANS_STATUS_APPLYREFUND)) {
			/**
			 * 买方申请退款，给卖方发邮件
			 */

			TransactionEmail tranMail = (TransactionEmail) IPBAppFactory
					.getObj("transactionMail");
			tranMail.setUrl(url);
			tranMail.mail(tran.getCredit());
			if (tt.ifSendMail(tran.getCredit().getAccount(),
					Constant.MONEY_BACK_NOTIFY))
				tranMail.sendMail(tran, null);
			if (tt.ifSendMobile(tran.getCredit().getAccount(),
					Constant.MONEY_BACK_NOTIFY)) {
				// 申请退款，收款方短信
				Map mapParam = new HashMap();
				mapParam.put("debit", WeatherUtil
						.getNameOrEmailOrMobileByAccount(tran.getDebit()
								.getAccount()));
				mapParam.put("time", now);
				tranMail.sendSms(tran, null,"sms-refund_credit.vm",mapParam);
			}
		} else if (tran.getTransStatus().equals(
				Constant.TRANS_STATUS_TRANSACTIONSUCCESS)
				&& tran.getActionType().getId().toString().equals(
						Constant.MODEL_TYPE_BANKDEPOSIT)) {
			/**
			 * 银行卡冲值成功
			 */
		} else if (tran.getTransStatus().equals(
				Constant.TRANS_STATUS_TRANSACTIONSUCCESS)
				&& tran.getActionType().getId().toString().equals(
						Constant.ACTION_TYPE_D)) {
			/**
			 * 提现交易成功
			 */
		} else if (tran.getTransStatus().equals(
				Constant.TRANS_STATUS_TRANSACTIONSUCCESS)
				&& tran.getActionType().getId().toString().equals(
						Constant.ACTION_TYPE_H)) {
			/**
			 * 内部交易
			 */
			SubAcctExMail sem = (SubAcctExMail) IPBAppFactory.getObj("subEx");
			if (tt.ifSendMail(tran.getDebit().getAccount(),
					Constant.SUBACCOUNT_EXCHAGE))
				sem.sendMail(tran, null);
			if (tt.ifSendMobile(tran.getDebit().getAccount(),
					Constant.SUBACCOUNT_EXCHAGE))
				sem.sendSms(tran, null);
		} else if (tran.getActionType().getId().toString().equals(
				Constant.ACTION_TYPE_C)) {
			/**
			 * 账户间交易
			 */

			if (tran.getTradeType().equals(Constant.TRADE_TYPE_NOASSURE)
					&& ((tran.getTransStatus()
							.equals(Constant.TRANS_STATUS_TRANSACTIONCLOSE)) || (tran
							.getTransStatus()
							.equals(Constant.TRANS_STATUS_TRANSACTIONFAIL)))) {
				CancelMail cancelmail = (CancelMail) IPBAppFactory
						.getObj("cancelMail");

				if (sub.getId().longValue() == tran.getCredit().getId()
						.longValue()) {
					cancelmail.setEmail(tran.getCredit());

					cancelmail.sendMail(tran, sh);

					// 取消收款，收款方(非担保)
					Map mapParam = new HashMap();
					mapParam.put("debit", WeatherUtil.getNameOrEmailOrMobileByAccount(tran
							.getDebit().getAccount()));
					mapParam.put("time", now);
					cancelmail.sendSms(tran, sh,"sms-innerreccancel_credit.vm",mapParam);
				} else {
					cancelmail.setEmail(tran.getDebit());
					if (tt.ifSendMail(tran.getDebit().getAccount(),
							Constant.GET_CANCEL_NOTIFY))
						cancelmail.sendMail(tran, sh);
					if (tt.ifSendMobile(tran.getDebit().getAccount(),
							Constant.GET_CANCEL_NOTIFY)) {
						// 取消收款，付款方(非担保)
						Map mapParam = new HashMap();
						mapParam.put("credit", WeatherUtil.getNameOrEmailOrMobileByAccount(tran
								.getCredit().getAccount()));
						cancelmail.sendSms(tran, sh,"sms-innerreccancel_debit.vm",mapParam);
					}
					cancelmail.setEmail(tran.getDebit());
				}

			} else if (tran.getTradeType().equals(Constant.TRADE_TYPE_NOASSURE)
					&& ((tran.getTransStatus()
							.equals(Constant.TRANS_STATUS_INIT)))) {
				TransactionEmail tranMail = (TransactionEmail) IPBAppFactory
						.getObj("transactionMail");
				if (sub.getId().longValue() == tran.getDebit().getId()
						.longValue()) {
					tranMail.mail(tran.getDebit());
				} else {
					tranMail.mail(tran.getCredit());
				}
				if (sub.getId().longValue() == tran.getDebit().getId()
						.longValue()) {
					if (tt.ifSendMail(tran.getDebit().getAccount(),
							Constant.GET_NOTIFY_BUY)) {
						tranMail.sendMail(tran, sh);
					}
					if (tt.ifSendMobile(tran.getDebit().getAccount(),
							Constant.GET_NOTIFY_BUY)) {
						// 账户间收款，付款方(非担保)
						Map mapParam = new HashMap();
						mapParam.put("time", now);
						mapParam.put("debit", WeatherUtil
								.getNameOrEmailOrMobileByAccount(tran
										.getDebit().getAccount()));
						mapParam.put("amount", tran.getAmount());
						tranMail.sendSms(tran, sh,"sms-innerreceive_debit.vm",mapParam);
					}
				} else {
					if (tt.ifSendMail(tranMail.getEmail().getAccount(),
							Constant.GET_NOTIFY_SALE))

						tranMail.sendMail(tran, sh);
					if (tt.ifSendMobile(tranMail.getEmail().getAccount(),
							Constant.GET_NOTIFY_SALE)) {
						// 账户间收款，收款方(非担保)
						Map mapParam = new HashMap();
						mapParam.put("time", now);
						mapParam.put("debit", WeatherUtil
								.getNameOrEmailOrMobileByAccount(tran
										.getDebit().getAccount()));
						mapParam.put("amount", tran.getAmount());
						tranMail.sendSms(tran, sh,"sms-innerreceive_credit.vm",mapParam);
					}
				}

			} else {
				TransactionEmail tranMail = (TransactionEmail) IPBAppFactory
						.getObj("transactionMail");
				tranMail.setUrl(url);
				if (sub.getId().longValue() == tran.getDebit().getId()
						.longValue()) {
					tranMail.mail(tran.getDebit());
				} else {
					tranMail.mail(tran.getCredit());
				}
				switch (Integer.parseInt(tran.getTransStatus())) {
				case 1:
					if (sub.getId().longValue() == tran.getDebit().getId()
							.longValue()) {
						if (tt.ifSendMail(tranMail.getEmail().getAccount(),
								Constant.PAY_SUCCESS_BUY))
							tranMail.sendMail(tran, sh);
						if (tt.ifSendMobile(tranMail.getEmail().getAccount(),
								Constant.PAY_SUCCESS_BUY)) {
							// 网上付款，付款方(担保)
							Map mapParam = new HashMap();
							mapParam.put("time", now);
							mapParam.put("credit", WeatherUtil
									.getNameOrEmailOrMobileByAccount(tran
											.getCredit().getAccount()));
							mapParam.put("amount", tran.getAmount());

							tranMail.sendSms(tran, sh,"sms-innerpay_debit.vm",mapParam);
						}
					} else {
						if (tt.ifSendMail(tranMail.getEmail().getAccount(),
								Constant.SALE_WAIT_SEND))

							tranMail.sendMail(tran, sh);
						if (tt.ifSendMobile(tranMail.getEmail().getAccount(),
								Constant.SALE_WAIT_SEND)) {
							// 网上付款，收款方(担保)
							Map mapParam = new HashMap();
							mapParam.put("debit", WeatherUtil
									.getNameOrEmailOrMobileByAccount(tran
											.getDebit().getAccount()));
							mapParam.put("time", now);
							mapParam.put("amount", tran.getAmount());
							tranMail.sendSms(tran, sh,"sms-innerpay_credit.vm",mapParam);
						}
					}
					break;
				case 2:
					if (sub.getId().longValue() == tran.getDebit().getId()
							.longValue()) {
						if (tt.ifSendMail(tranMail.getEmail().getAccount(),
								Constant.PAY_WAITING_))
							tranMail.sendMail(tran, sh);
						if (tt.ifSendMobile(tranMail.getEmail().getAccount(),
								Constant.PAY_WAITING_)) {
							// 已发货，付款方(买)
							Map mapParam = new HashMap();
							mapParam.put("credit", WeatherUtil
									.getNameOrEmailOrMobileByAccount(tran
											.getDebit().getAccount()));
							mapParam.put("time", now);
							tranMail.sendSms(tran, sh,"sms-shipped_debit.vm",mapParam);
						}
					} else {
						if (tt.ifSendMail(tranMail.getEmail().getAccount(),
								Constant.SALE_SEND))

							tranMail.sendMail(tran, sh);
						if (tt.ifSendMobile(tranMail.getEmail().getAccount(),
								Constant.SALE_SEND)) {
							// 已发货，收款方(卖)
							Map mapParam = new HashMap();
							mapParam.put("debit", WeatherUtil
									.getNameOrEmailOrMobileByAccount(tran
											.getDebit().getAccount()));
							mapParam.put("time", now);
							tranMail.sendSms(tran, sh,"sms-shipped_credit.vm",mapParam);
						}
					}
					break;
				case 3:
					if (sub.getId().longValue() == tran.getDebit().getId()
							.longValue()) {
						if (tt.ifSendMail(tranMail.getEmail().getAccount(),
								Constant.BUY_TRAN_SUCCESS))

							tranMail.sendMail(tran, sh);
						if (tt.ifSendMobile(tranMail.getEmail().getAccount(),
								Constant.BUY_TRAN_SUCCESS)) {
							if (tran.getCredit().getAccount().getAccountType()
									.equals(Constant.ACCOUNT_TYPE_INITIAL)) {
								// 内部账户短信通知
								Map mapParam = new HashMap();
								mapParam.put("time", now);
								mapParam.put("amount", tran.getAmount());
								tranMail.sendSms(tran, sh,"sms-innerpay_inneracct.vm",mapParam);
							} else {
								// 网上付款，付款方(非担保)
								Map mapParam = new HashMap();
								mapParam.put("time", now);
								mapParam.put("credit", WeatherUtil
										.getNameOrEmailOrMobileByAccount(tran
												.getCredit().getAccount()));
								mapParam.put("amount", tran.getAmount());

								tranMail.sendSms(tran, sh,"sms-innerpay_debit.vm",mapParam);
							}
						}
					} else {
						if (tt.ifSendMail(tranMail.getEmail().getAccount(),
								Constant.SALE_TRAN_SUCCESS))

							tranMail.sendMail(tran, sh);
						if (tt.ifSendMobile(tranMail.getEmail().getAccount(),
								Constant.SALE_TRAN_SUCCESS)) {
							// 网上付款，收款方(非担保)
							Map mapParam = new HashMap();
							mapParam.put("debit", WeatherUtil
									.getNameOrEmailOrMobileByAccount(tran
											.getDebit().getAccount()));
							mapParam.put("time", now);
							mapParam.put("amount", tran.getAmount());
							tranMail.sendSms(tran, sh,"sms-innerpay_credit.vm",mapParam);
						}
					}
					break;
				case 5:
					if (sub.getId().longValue() == tran.getDebit().getId()
							.longValue()) {
						if (tt.ifSendMail(tran.getDebit().getAccount(),
								Constant.GET_NOTIFY_BUY))
							tranMail.sendMail(tran, sh);
						if (tt.ifSendMobile(tran.getDebit().getAccount(),
								Constant.GET_NOTIFY_BUY)) {
							// 账户间收款，付款方(担保)
							Map mapParam = new HashMap();
							mapParam.put("time", now);
							mapParam.put("debit", WeatherUtil
									.getNameOrEmailOrMobileByAccount(tran
											.getDebit().getAccount()));
							mapParam.put("amount", tran.getAmount());
							tranMail.sendSms(tran, sh,"sms-innerreceive_debit.vm",mapParam);
						}
					} else {
						if (tt.ifSendMail(tran.getCredit().getAccount(),
								Constant.GET_NOTIFY_SALE))
							tranMail.sendMail(tran, sh);
						if (tt.ifSendMobile(tran.getCredit().getAccount(),
								Constant.GET_NOTIFY_SALE)) {
							// 账户间收款，收款方(担保)
							Map mapParam = new HashMap();
							mapParam.put("time", now);
							mapParam.put("debit", WeatherUtil
									.getNameOrEmailOrMobileByAccount(tran
											.getDebit().getAccount()));
							mapParam.put("amount", tran.getAmount());
							tranMail.sendSms(tran, sh,"sms-innerreceive_credit.vm",mapParam);
						}
					}
					break;
				case 6:
					if (sub.getId().longValue() == tran.getDebit().getId()
							.longValue()) {
						if (tt.ifSendMail(tranMail.getEmail().getAccount(),
								Constant.GET_CANCEL_NOTIFY))
							tranMail.sendMail(tran, sh);
						if (tt.ifSendMobile(tranMail.getEmail().getAccount(),
								Constant.GET_CANCEL_NOTIFY)) {
							if (tran.getUpdateUser().equals(
									tran.getDebit().getAccount().getId()
											.toString())) {
								// 取消收款，付款方(担保交易)
								Map mapParam = new HashMap();
								mapParam.put("credit", WeatherUtil
										.getNameOrEmailOrMobileByAccount(tran
												.getCredit().getAccount()));
								tranMail.sendSms(tran, sh,"sms-innerreccancel_debit.vm",mapParam);
							}
							else {
								// 收款方取消收款，付款方短信(担保)
								Map mapParam = new HashMap();
								mapParam.put("credit", WeatherUtil
										.getNameOrEmailOrMobileByAccount(tran
												.getCredit().getAccount()));
								mapParam.put("time", now);
								tranMail.sendSms(tran, sh,"sms-rec_c_cancel_assure_d.vm",mapParam);
							}
						}
					} else {
						tranMail.sendMail(tran, sh);
						if (tran.getUpdateUser()
								.equals(
										tran.getDebit().getAccount().getId()
												.toString())) {
							// 付款放取消收款，收款方(担保交易)
							Map mapParam = new HashMap();
							mapParam.put("dredit", WeatherUtil
									.getNameOrEmailOrMobileByAccount(tran
											.getCredit().getAccount()));
							mapParam.put("time", now);
							tranMail.sendSms(tran, sh,"sms-innerreccancel_credit.vm",mapParam);
							
						}
						else {
							// 收款方取消收款，收款方短信(担保)
							Map mapParam = new HashMap();
							mapParam.put("debit", WeatherUtil
									.getNameOrEmailOrMobileByAccount(tran
											.getCredit().getAccount()));
							mapParam.put("time", now);
							tranMail.sendSms(tran, sh,"sms-rec_c_cancel_assure_c.vm",mapParam);
						}
						
					}
				}
			}

			mail.setContext(vnvMap);

		}
	}
}
